﻿package mortal.game.manager.msgTip
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.utils.pools.*;
    import com.greensock.*;
    import com.mui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.game.cache.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.chat.*;
    import mortal.game.view.chat.chatPanel.*;
    import mortal.game.view.chat.chatViewData.*;
    import mortal.game.view.common.*;

    public class SpeakerItem extends Sprite
    {
        private var _maskSp:Sprite;
        private var _itemSprite:Sprite;
        private var _chatItem:ChatItem;
        private var _endFun:Function;
        private var _inTween:TweenMax;
        private var _outTween:TweenMax;
        private var _timerKey:uint;
        private var _delay:Number;
        private var _level:int;
        private var _swfPlayerBg:SWFPlayer;
        private var _swfPlayerShine:SWFPlayer;
        private var _miniPlayer:SMiniPlayer;
        private var _bg:GImageBitmap;

        public function SpeakerItem()
        {
            this._maskSp = new Sprite();
            this._maskSp.graphics.beginFill(16777215, 0.1);
            this._maskSp.graphics.drawRect(0, 0, 300, 102);
            this._maskSp.graphics.endFill();
            this._maskSp.y = -10;
            this._maskSp.mouseChildren = false;
            this._maskSp.mouseEnabled = false;
            this.addChild(this._maskSp);
            this._itemSprite = new Sprite();
            this._itemSprite.x = 0;
            this._itemSprite.mouseEnabled = false;
            this._itemSprite.mask = this._maskSp;
            this.addChild(this._itemSprite);
            mouseChildren = true;
            mouseEnabled = false;
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            return;
        }// end function

        private function onAddToStage(event:Event) : void
        {
            if (this._chatItem && this._itemSprite.contains(this._chatItem))
            {
                this._itemSprite.removeChild(this._chatItem);
            }
            if (!this._swfPlayerBg)
            {
                this._swfPlayerBg = UIFactory.getUICompoment(SWFPlayer);
                this._swfPlayerBg.timeRate = 2;
                this._swfPlayerBg.move(0, 0);
                this._swfPlayerBg.load("ChatTrumpetBg.swf", ModelType.NormalSwf, null);
            }
            this._swfPlayerBg.play();
            this._itemSprite.addChildAt(this._swfPlayerBg, 0);
            if (!this._swfPlayerShine)
            {
                this._swfPlayerShine = UIFactory.getUICompoment(SWFPlayer);
                this._swfPlayerShine.playNum = 1;
                this._swfPlayerShine.timeRate = 2;
                this._swfPlayerShine.move(0, 7);
                this._swfPlayerShine.load("ChatTrumpetShine.swf", ModelType.NormalSwf, null);
            }
            this._swfPlayerShine.actionPlayCompleteHandler = this.shineCompl;
            this._swfPlayerShine.play();
            this._itemSprite.addChild(this._swfPlayerShine);
            return;
        }// end function

        private function shineCompl() : void
        {
            if (this._chatItem)
            {
                this._itemSprite.addChild(this._chatItem);
            }
            return;
        }// end function

        private function onRemoveFromStage(event:Event) : void
        {
            this.disposeSWF();
            return;
        }// end function

        private function disposeSWF() : void
        {
            if (this._swfPlayerBg)
            {
                this._swfPlayerBg.dispose();
                this._swfPlayerBg = null;
            }
            if (this._swfPlayerShine)
            {
                this._swfPlayerShine.dispose();
                this._swfPlayerShine = null;
            }
            return;
        }// end function

        public function init(param1:SMiniPlayer, param2:String, param3:MsgTypeImpl) : void
        {
            this._miniPlayer = param1;
            this._level = Cache.instance.role.entityInfo.level;
            this._delay = param3.delay;
            var _loc_4:* = param3.color;
            var _loc_5:* = ObjectPool.getObject(ChatItemData);
            _loc_5.type = ChatMessageWorking.getChatTypeByAreaName(ChatArea.Speaker);
            var _loc_6:* = new Vector.<ChatCellData>;
            var _loc_7:int = 3;
            if (VipConfig.instance.isShowEffect(EVipRecordType._EVipRecordTypeExpress, param1.VIP))
            {
                _loc_7 = 3;
            }
            else
            {
                _loc_7 = 1;
            }
            _loc_6 = _loc_6.concat(ChatMessageWorking.getPlayerCellData(param1));
            _loc_6 = _loc_6.concat(ChatMessageWorking.getCellDataByContent(param2, parseInt(_loc_4), true, 12, _loc_7));
            _loc_5.cellVector = _loc_6;
            this._chatItem = ObjectPool.getObject(ChatItem);
            this._chatItem.init(_loc_5.getContentElement(), 240);
            this._chatItem.x = 15;
            this._chatItem.y = 10;
            this._itemSprite.addChild(this._chatItem);
            return;
        }// end function

        private function onInEnd() : void
        {
            this._timerKey = setTimeout(this.timerOutHandler, this._delay);
            return;
        }// end function

        private function onOutEnd() : void
        {
            if (this._endFun != null)
            {
                this._endFun.call(this, this);
            }
            return;
        }// end function

        private function timerOutHandler() : void
        {
            this._outTween = TweenMax.to(this._itemSprite, 0.1, {alpha:0, onComplete:this.onOutEnd});
            return;
        }// end function

        public function updateData(param1:Function, param2:Boolean = true) : void
        {
            this._endFun = param1;
            if (param2)
            {
                this._itemSprite.alpha = 0;
                this._itemSprite.y = 20;
                this._inTween = TweenMax.to(this._itemSprite, 0.1, {alpha:1, y:0, onComplete:this.onInEnd});
            }
            else
            {
                this._itemSprite.alpha = 1;
                this._itemSprite.y = 0;
                this.onInEnd();
            }
            return;
        }// end function

        public function killInEase() : void
        {
            if (this._inTween && this._inTween.active)
            {
                this._itemSprite.y = 0;
                this._itemSprite.alpha = 1;
                this._inTween.kill();
                this.onInEnd();
            }
            return;
        }// end function

        public function get miniPlayer() : SMiniPlayer
        {
            return this._miniPlayer;
        }// end function

        public function dispose() : void
        {
            this._endFun = null;
            this._miniPlayer = null;
            clearTimeout(this._timerKey);
            if (this._inTween && this._inTween.active)
            {
                this._inTween.kill();
            }
            else if (this._outTween && this._outTween.active)
            {
                this._outTween.kill();
            }
            if (this._itemSprite)
            {
                this._itemSprite.alpha = 1;
                this._itemSprite.y = 0;
                if (this._chatItem && this._chatItem.parent)
                {
                    this._itemSprite.removeChild(this._chatItem);
                }
            }
            this.disposeSWF();
            return;
        }// end function

        override public function get height() : Number
        {
            return this._itemSprite.height;
        }// end function

        override public function get width() : Number
        {
            return this._itemSprite.width;
        }// end function

    }
}
